package com.jd.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.jd.beans.Goods;
import com.jd.util.Constants;

public class RequestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doPost(req, resp);
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String flag = request.getParameter("flag");
		String type = request.getParameter("type");
		//群众眼光列表
		if (flag.equals("list")) {
			List<Goods> goodsList = new ArrayList<Goods>();
			for (int i = 0; i < Constants.id.length; i++) {
				Goods goods = new Goods();
				goods.setId(Constants.id[i]);
				goods.setTitle(Constants.title[i]);
				goods.setImageUrl(Constants.imageUrl[i]);
				goods.setPrice(Constants.price[i]);
				goods.setStars(Constants.stars[i]);
				StringBuffer buttonStr = new StringBuffer();
				int actCount = 0;
				for(int j = 0; j < Constants.buttons.length; j++)  
		        {   
					
					Random random = new Random();
					boolean isSet = random.nextBoolean();
					int count = random.nextInt(10) + 1;
					if (j == 5 || j == 6) 
						isSet = true;
					if (isSet) {
						buttonStr.append("<a href='../../RequestServlet?flag=")
								 .append(Constants.buttons[j][2])
								 .append("&id=")
								 .append(Constants.id[i])
								 .append("'>")
								 .append("<img src='http://223.203.189.221:28080/jd/demo/template/images/")
								 .append(Constants.buttons[j][1])
								 .append("'></img>");
						if (j != 5 && j != 6) {
							buttonStr.append(count);
							actCount++;
						}
						buttonStr.append("</a> ");
					}
		        }
				goods.setActCount(actCount + "");
				goods.setButtons(buttonStr.toString());
				goodsList.add(goods);
			}
			if (type.equals("act") || type.equals("sales")) {
				java.util.Collections.sort(goodsList);
			}
			JSONArray array = JSONArray.fromObject(goodsList);
			response.setContentType("text/html; charset=utf-8");
			PrintWriter out = response.getWriter();
			out.println(array.toString());
		}
		//自由扫货列表
		else if (flag.equals("list2")) {
			List<Goods> goodsList = new ArrayList<Goods>();
			for (int i = 0; i < Constants.id.length; i++) {
				Goods goods = new Goods();
				goods.setId(Constants.id[i]);
				goods.setTitle(Constants.title[i]);
				goods.setImageUrl(Constants.imageUrl[i]);
				goods.setPrice(Constants.price[i]);
				goods.setStars(Constants.stars[i]);
				StringBuffer buttonStr = new StringBuffer();
				for(int j = 0; j < Constants.buttons1.length; j++)  
		        {  
					Random random = new Random();
					boolean isSet = random.nextBoolean();
					if (j == 1 || j == 2) 
						isSet = true;
					if (isSet) {
						buttonStr.append("<a href='../../RequestServlet?flag=")
								 .append(Constants.buttons1[j][2])
								 .append("&id=")
								 .append(Constants.id[i])
								 .append("'>")
								 .append("<img src='http://223.203.189.221:28080/jd/demo/template/images/")
								 .append(Constants.buttons1[j][1])
								 .append("'></img>")
								 .append("</a> ");
					}
		        }
				goods.setButtons(buttonStr.toString());
				goodsList.add(goods);
			}
			JSONArray array = JSONArray.fromObject(goodsList);
			response.setContentType("text/html; charset=utf-8");
			PrintWriter out = response.getWriter();
			out.println(array.toString());
		}
		//详情
		else if (flag.equals("one")) {
			String id = request.getParameter("id");
			Goods goods = new Goods();
			goods.setId(id);
			goods.setTitle(Constants.title[Integer.parseInt(id) - 1]);
			goods.setImageUrl(Constants.imageUrl[Integer.parseInt(id) - 1]);
			goods.setPrice(Constants.price[Integer.parseInt(id) - 1]);
			goods.setStars(Constants.stars[Integer.parseInt(id) - 1]);
			JSONObject obj = JSONObject.fromObject(goods);
			request.setAttribute("obj", obj);
			request.getRequestDispatcher("/demo/detail.jsp").forward(request,response); 
		} 
		//支付
		else if (flag.equals("pay")) {
			String id = request.getParameter("id");
			Goods goods = new Goods();
			goods.setId(id);
			goods.setTitle(Constants.title[Integer.parseInt(id) - 1]);
			goods.setImageUrl(Constants.imageUrl[Integer.parseInt(id) - 1]);
			goods.setPrice(Constants.price[Integer.parseInt(id) - 1]);
			goods.setStars(Constants.stars[Integer.parseInt(id) - 1]);
			JSONObject obj = JSONObject.fromObject(goods);
			request.setAttribute("obj", obj);
			request.getRequestDispatcher("/demo/pay.jsp").forward(request,response); 
		} 
		//左右 7大按钮
		else {
			String id = request.getParameter("id");
			Goods goods = new Goods();
			goods.setId(id);
			goods.setTitle(Constants.title[Integer.parseInt(id) - 1]);
			goods.setImageUrl(Constants.imageUrl[Integer.parseInt(id) - 1]);
			goods.setPrice(Constants.price[Integer.parseInt(id) - 1]);
			goods.setRatePrice((Double.parseDouble((Constants.price[Integer.parseInt(id) - 1])) * 0.9) + "");
			goods.setStars(Constants.stars[Integer.parseInt(id) - 1]);
			JSONObject obj = JSONObject.fromObject(goods);
			request.setAttribute("obj", obj);
			request.getRequestDispatcher("/demo/" + flag +".jsp").forward(request,response); 
		}
	}
	
	public static void main(String[] args) {
		Random ra =new Random();
		for (int i=0;i<30;i++)
		{System.out.println(ra.nextInt(10)+1);}
	}
	
}
